package com.ruoyi.media.invitationCode.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.media.invitationCode.domain.MediaInvitationCode;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.media.invitationCode.domain.vo.MediaInvitationCodeVo;
import com.ruoyi.media.invitationCode.domain.bo.MediaInvitationCodeBo;
import com.ruoyi.media.invitationCode.service.IMediaInvitationCodeService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户邀请码
 *
 * @author dqw
 * @date 2023-05-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/invitationCode/invitationCode")
public class MediaInvitationCodeController extends BaseController {

    private final IMediaInvitationCodeService iMediaInvitationCodeService;

    /**
     * 查询用户邀请码列表
     */
    @GetMapping("/list")
    public TableDataInfo<MediaInvitationCodeVo> list(MediaInvitationCodeBo bo, PageQuery pageQuery) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        bo.setUseUserName(loginUser.getUsername());
        return iMediaInvitationCodeService.queryPageList(bo, pageQuery);
    }


    /**
     * 查询用户邀请码列表
     */
    @GetMapping("/listAll")
    public TableDataInfo<MediaInvitationCodeVo> listAll(MediaInvitationCodeBo bo, PageQuery pageQuery) {
        return iMediaInvitationCodeService.queryPageList(bo, pageQuery);
    }


    /**
     * 导出用户邀请码列表
     */
    @SaCheckPermission("invitationCode:invitationCode:export")
    @Log(title = "用户邀请码", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(MediaInvitationCodeBo bo, HttpServletResponse response) {
        List<MediaInvitationCodeVo> list = iMediaInvitationCodeService.queryList(bo);
        ExcelUtil.exportExcel(list, "用户邀请码", MediaInvitationCodeVo.class, response);
    }

    /**
     * 获取用户邀请码详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("invitationCode:invitationCode:query")
    @GetMapping("/{id}")
    public R<MediaInvitationCodeVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String id) {
        return R.ok(iMediaInvitationCodeService.queryById(id));
    }

    /**
     * 新增用户邀请码
     */
    @SaCheckPermission("invitationCode:invitationCode:add")
    @Log(title = "用户邀请码", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody MediaInvitationCodeBo bo) {
        iMediaInvitationCodeService.createInvitationCode(bo.getValidTime());
        return toAjax(true);
    }

    /**
     * 修改用户邀请码
     */
    @SaCheckPermission("invitationCode:invitationCode:edit")
    @Log(title = "用户邀请码", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody MediaInvitationCodeBo bo) {
        return toAjax(iMediaInvitationCodeService.updateByBo(bo));
    }

    /**
     * 删除用户邀请码
     *
     * @param ids 主键串
     */
    @SaCheckPermission("invitationCode:invitationCode:remove")
    @Log(title = "用户邀请码", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(iMediaInvitationCodeService.deleteWithValidByIds(Arrays.asList(ids), true));
    }


    /**
     * 京东小店获取授权
     */
    @GetMapping("/jdsq")
    public R<?> jdsq() {
        boolean flag = false;
        MediaInvitationCode mediaInvitationCode = iMediaInvitationCodeService.getById("jdxd");
        int res =  mediaInvitationCode.getCount();
        if(res > 0){
            flag = true;
            MediaInvitationCodeBo bo = new MediaInvitationCodeBo();
            bo.setId("jdxd");
            bo.setCount(res - 1);
            iMediaInvitationCodeService.updateByBo(bo);
        }
        return R.ok(res);
    }



}
